package com.xm.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.xm.config.AlipayConfig;

/**
 * @author xie'lin'feng
 * @date 2020-08-25-13:58
 * @description 支付宝支付工具类
 */
public class AlipayUtil {
    public static String pay(String orderNo,double total){
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        String out_trade_no = orderNo;
        String total_amount = String.valueOf(total);
        String subject = "orange商城";
        String body = "支付宝值得信赖的支付平台";
        String timeout_express = "3m";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\",\"total_amount\":\"" + total_amount + "\",\"subject\":\"" + subject + "\",\"body\":\"" + body + "\",\"timeout_express\":\"" + timeout_express + "\",\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String jg = null;
        try {
            jg = ((AlipayTradePagePayResponse)alipayClient.pageExecute(alipayRequest)).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }finally {
            return jg;
        }

    }
}
